#include "InvokeTest.h"

#include "../x-gate-util/Invoker/Invokable.h"
#include "../x-gate-util/Threading/Thread.h"

using namespace xgu;

#include <iostream>
using namespace std;

// ---------------------------------------------

class CInvokeTest : public xgu::Invokable
{
public:
	void foo()
	{
		cout << "CInvokeTest::foo() called from thread: " << Thread::getTid() << endl;
	}
	void foo(int i)
	{
		cout << "CInvokeTest::foo(int i) called from thread: " << Thread::getTid() 
			<< " with parameter i: " << i << endl;
	}
	void Run()
	{
		cout << "Starting thread " << Thread::getTid() << endl;
		BeginInvoke(Delegate<void>(*this, &CInvokeTest::foo));
		BeginInvoke(Delegate<void>(*this, &CInvokeTest::foo, 1));
		BeginInvoke(Delegate<void>(*this, &CInvokeTest::foo, 2));
		BeginInvoke(Delegate<void>(*this, &CInvokeTest::foo, 3));
		BeginInvoke(Delegate<void>(*this, &CInvokeTest::foo, 4));
		cout << "Ending thread " << Thread::getTid() << endl;
	}
};

void InvokeTest()
{
	CInvokeTest invoke_test;

	cout << "----------------------- Invoke Test -----------------------" << endl;
	cout << "Invocation target thread: " << Thread::getTid() << endl;
	Thread* thread = new Thread(ThreadStart(invoke_test, &CInvokeTest::Run));
	thread->Start();

	for (int i = 0; i < 3000; ++i)
	{
		Invokable::DoEvents();
		Sleep(0);
	}

	delete thread;
}
